package org.poem.aware;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Created by poem on 2017/8/27.
 */
@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware {

    private String beanName;

    private ResourceLoader resourceLoader;

    /**
     * 重写setBeanName方法
     * @param s
     */
    @Override
    public void setBeanName(String s) {
        this.beanName = s;
    }

    /**
     * 重写ResourceLoader
     * @param resourceLoader
     */
    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public void println(){
        System.err.println("Bean的名称为："+beanName);
        Resource resource = resourceLoader.getResource("classpath:resources/README.txt");
        try{
            System.err.println("ResourceLoader加载的文件内容为："+ IOUtils.toString(resource.getInputStream()));
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
